package com.aceitunaproject.server.entity;

import java.awt.Point;

import com.aceitunaproject.server.player.Player;
import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.ManagedReference;

/**
 * @author Federico Bergstein
 * 
 *         Provide some basic functionality for all classes that represent game
 *         entities (Units,Installations,Settlements)
 */
public abstract class BasicEntity implements Entity {

	/**
	 * Serial UID for serialization purposes
	 */

	private static final long serialVersionUID = -7723520972400039417L;
	private Long id;
	private String name;
	private ManagedReference<Player> ownerRef;
	private ManagedReference<Player> controllerRef;
	private Point currentPosition;
	
	


	public BasicEntity(Long id, String name, Player owner, Player controller,Point currentPosition) {
		this.id = id;
		this.name = name;
		this.ownerRef = AppContext.getDataManager().createReference(owner);
		this.controllerRef = AppContext.getDataManager().createReference(controller);
		this.currentPosition = currentPosition;
	}

	@Override
	public long getId() {
		return id;
	}

	@Override
	public void setId(Long id) {
		AppContext.getDataManager().markForUpdate(this);
		this.id = id;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		AppContext.getDataManager().markForUpdate(this);
		this.name = name;
	}


	@Override
	public Player getOwner() {
		return ownerRef.get();
	}
	
	@Override
	public Player getController() {
		return controllerRef.get();
	}

	@Override
	public void setCurrentPosition(Point currentPosition) {
		AppContext.getDataManager().markForUpdate(this);
		this.currentPosition = currentPosition;
	}

	@Override
	public Point getCurrentPosition() {
		return currentPosition;
	}
	
	
}